import { loadMdicMap } from '@/services/BaseLayout';
import { getBaseUrl } from '@/services/urlconst';
import { getTenantID, requestFunComponFiex } from '@/utils';
import React from 'react';
import { useRequest } from 'umi';
import { ContacterAPI } from '../common/type';
import { getSearchList, queryTypeObj } from '../mconfig';
import { ILoadMapTypeAll } from '@/common';

const baseUrl = getBaseUrl();
// const baseUrl = 'http://192.168.153.246:31019'; //旭潮本地

const contacterManageService = (curRes: Record<string, any>) => {
  const loadStrList: string[] = [
    `${getTenantID()}${ILoadMapTypeAll.NATION_TYPE}`,
    `${getTenantID()}${ILoadMapTypeAll.EDUCATION_LEVEL}`,
    `${getTenantID()}${ILoadMapTypeAll.POLITICAL_OUTLOOK}`,
  ];
  /** 获取数据字典loadMdicMap */
  const loadMdicMapList = useRequest(() => {
    return loadMdicMap(loadStrList);
  });

  /** 获取权限参数 */
  const queryInfoObj: Record<string, any> = queryTypeObj(curRes);

  /** 获取短信应用数据 */
  const { tableProps, data, refresh, loading, run } = useRequest(
    (opt: ContacterAPI.IGridRequestType) => {
      let requestURL = `${baseUrl}/supplierContacterController/dataGrid/${queryInfoObj?.params}`;

      let bodyParams: any = {
        current: 1,
        pageSize: 10,
        sort: 'createTime',
        order: 'desc',
        dicTypeIds: loadStrList?.toString(),
        ...queryInfoObj,
        ...opt,
      };
      return requestFunComponFiex('POST', requestURL, bodyParams);
    },
    {
      manual: true, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /* 获取页面数据 */
  const contacterTableData = (otherRequest: ContacterAPI.IGridRequestType | any) => {
    const request: ContacterAPI.IGridRequestType | any = {
      ...otherRequest,
    };

    if (request?.createTime) {
      const [createTimeStart, createTimeEnd] = request.createTime;
      request['createTimeStart'] = createTimeStart.format('YYYY-MM-DD HH:mm:ss');
      request['createTimeEnd'] = createTimeEnd.format('YYYY-MM-DD HH:mm:ss');
      delete request['createTime'];
    }

    delete request['createFormNameDept'];
    delete request['createFormNameUser'];

    if (request?.createrID && request?.createrID?.length > 0) {
      request['createrID'] = request?.createrID?.toString();
    } else {
      delete request['createrID'];
    }

    run({
      ...request,
    });
  };

  /* 页面列表 */
  let contacterTableProps = React.useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  /** 搜索列表  */
  let searchList = React.useMemo(() => {
    return {
      searchList: getSearchList(loadMdicMapList, queryInfoObj),
    };
  }, [loadMdicMapList, queryInfoObj]);

  return {
    loading,
    contacterTableData, //其他请求操作方法例如刷新等等
    refreshManage: refresh,
    ...contacterTableProps, //页面列表值
    ...searchList,
  };
};

export { contacterManageService };
